package com.shuoyue.adven.main.bussiness;

import android.os.Bundle;
import android.os.Handler;

import androidx.viewpager.widget.ViewPager;

import com.shuoyue.adven.Constant;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.Adv;
import com.shuoyue.adven.entity.ImpotentResult;
import com.shuoyue.adven.entity.Property;
import com.sm.appbase.base.basemvp.BaseMvpFragment;

import java.util.List;

import butterknife.BindView;

public class FragmentPropertyAdv extends BaseMvpFragment<MainContract.Presenter> implements MainContract.View {

    @BindView(R.id.imgs_pager)
    public ViewPager imgsPager;

    Handler handler;
    Runnable netTask;
    Runnable scrolltask;
    public MyPagerAdapter mGalleryAdapter;

    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.propery_adv;
    }

    @Override
    protected void initView() {
        mGalleryAdapter = new MyPagerAdapter(mContext, null);
        imgsPager.setAdapter(mGalleryAdapter);
    }

    @Override
    public void initData() {
        mPresenter = new MainContract.Presenter();
        mPresenter.attachView(this);
        initTimerTask();
        mPresenter.queryPropertyAdv();
    }

    void initTimerTask() {
        handler = new Handler();
        netTask = () -> {
            handler.postDelayed(netTask, Constant.NET_TASK * 1000);
            mPresenter.queryPropertyAdv();
        };
        scrolltask = () -> {
            handler.postDelayed(scrolltask, Constant.SUB_SCREEN_SCROLL_DELAY);
            if (imgsPager != null)
                imgsPager.setCurrentItem(imgsPager.getCurrentItem() + 1);
        };
        mPresenter.queryPropertyAdv();
        handler.postDelayed(netTask, Constant.NET_TASK * 1000);
    }


    void startScroll() {
        handler.postDelayed(scrolltask, Constant.SUB_SCREEN_SCROLL_DELAY);
    }

    void stopScroll() {
        handler.removeCallbacks(scrolltask);
    }


    @Override
    public void queryBusinessAdvSuc(List<Adv> mData) {

    }

    @Override
    public void queryPropertyAdvSuc(List<Adv> videoList) {
        if (mGalleryAdapter == null) {
            mGalleryAdapter = new MyPagerAdapter(mContext, null);
        }
        mGalleryAdapter.resetData(videoList);

        if (videoList.size() > 1) {
            handler.removeCallbacks(scrolltask);
            startScroll();
        } else {
            stopScroll();
        }
    }

    @Override
    public void queryImpotentAdvSuc(ImpotentResult mdata) {
    }

    @Override
    public void getPropertyInfoSuc(Property propertyInfo) {
    }

    @Override
    public void onDestroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();

    }
}
